// pages/studentPage/studentPage.js

var Mcaptcha = require('../../utils/mcaptcha.js');
var app=getApp();

Page({

  
  data: {
      studentId:undefined,
      password:undefined,
      bindCode: undefined,
      codeFlag: -2,
      name:""
  },

  getBindCode: function (e) {
    this.setData({
      bindCode: e.detail.value
    })
    this.checkBindCode();
  },
  checkBindCode: function () {
    var that = this
    var iscode = this.mcaptcha.validate(this.data.bindCode);
    if (this.data.bindCode == undefined) {
      that.setData({
        codeFlag: 0
      })
    } else if (iscode) {
      that.setData({
        codeFlag: 1
      })
    } else {
      that.setData({
        codeFlag: -1
      })
    }
    console.log(this.data.codeFlag);
  },
  changeBind() {
    this.mcaptcha.refresh();
  },
  goRegist:function(e){
    wx.redirectTo({
      url: '/pages/studentRegistPage/studentRegistPage',
    })
  },

  getStudentId:function(e){
    console.log(e.detail.value)
    this.setData({
      studentId: e.detail.value
    })
  },
  getpassword: function (e) {
    console.log(e.detail.value)
    this.setData({
      password: e.detail.value
    })
  },



  login: function (e) {
      if (this.data.studentId && this.data.password&&this.data.bindCode) {
        if(this.data.codeFlag===0){
          wx.showToast({
            title: '请输入验证码',
            icon: "none"
          })
        }
        else if(this.data.codeFlag==-1){
          wx.showToast({
            title: '验证码错误',
            icon: "none"
          })
          this.changeBind();
        }
        else if(this.data.codeFlag==1){
          var that=this
          wx.request({  //这一步发起网络请求 
            url: 'https://weixin0304.cn/studentLogin.php',


            data: {
              studentId: this.data.studentId,
              password: this.data.password,
            },
            header: { //'Content-Type': 'application/json'
              'content-type': 'application/json;charset=utf-8'
            },
            method: 'GET',

            success: function (res) {
              console.log(res);
            
              //result=1表示登录成功
              if (res.data[0] == 1) {
               /* app.globalData.name=res.data[1][3];
                app.globalData.id = res.data[1][0];*/
                var name1 = res.data[1][3]
                
                console.log(that.data.studentId)
                wx.setStorageSync('id', that.data.studentId);
                wx.setStorageSync('name', res.data[1][3]);
                wx.setStorageSync('role', "学生");
                wx.redirectTo({
                  url: "/pages/studentAcPage/studentAcPage?id="+that.data.studentId+"&name="+name1,
                })
            
                wx.set
              } else {
                wx.showModal({
                  title: '温馨提示',
                  content: '用户名密码错误',
                  showCancel: false,
                  success: function (res) {

                  }
                })
              }
              console.log('submit success');
            },
            fail: function (res) {
              wx.showToast({
                title: '您的网络开小差啦~~~',
                icon: "none"
              })
            },
            complete: function (res) {
              console.log('submit complete');
            }
          })
        }
    } else {
      wx.showToast({
        title: '请将信息填写完整后提交',
        icon: "none"
      })
    }
  },

  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.mcaptcha = new Mcaptcha({
      el: 'canvas',
      width: 80,
      height: 35,
      createCodeImg: ""
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})